package org.csu.mypetstore.web.servlet;
import org.csu.mypetstore.domain.Account;
import org.csu.mypetstore.service.AccountService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "ViewUpdateAccountServlet", value = "/ViewUpdateAccountServlet")
public class ViewUpdateAccountServlet extends HttpServlet {
    private static String UPDATE_ACCOUNT="/WEB-INF/jsp/account/UpdateAccountForm.jsp";
    private static String MAIN="/WEB-INF/jsp/catalog/Main.jsp";
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        AccountService accountService=new AccountService();
        //System.out.println(((Account)session.getAttribute("accounts")).getStatus());
        //先得到Account
        Account account=new Account();
        account=(Account) session.getAttribute("accounts");
        if(account!=null)
        {
            String s=account.getUsername();
            accountService.updateUsername(s);
            if(((Account)session.getAttribute("accounts")).getStatus()!=null)
            {
                request.getRequestDispatcher(UPDATE_ACCOUNT).forward(request,response);
            }
            else
            {
                session.setAttribute("error","您并未登陆，无法修改信息");
                request.getRequestDispatcher(MAIN).forward(request,response);
            }
        }
        else
        {
            request.getRequestDispatcher(MAIN).forward(request,response);
        }

    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }
}
